package com.hrcx.mystery.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;


/**
 * 控制层切面,记录请求的时长
 *
 * @author LinXiPeng
 */
@Aspect
@Component
@Slf4j
public class ControllerAspect {

    @Pointcut("execution(* com.hrcx.mystery.controller.*.*(..))")
    private void pointCutMethodController() {
    }

    @Around("pointCutMethodController()")
    public Object doAroundService(ProceedingJoinPoint joinPoint) throws Throwable {

        long begin = System.nanoTime();

        Object obj = joinPoint.proceed();

        long end = System.nanoTime();

        log.info("Controller method：{}，prams：{}，cost time：{} ns，cost：{} ms",
                joinPoint.getSignature().toString(), Arrays.toString(joinPoint.getArgs()), (end - begin), (end - begin) / 1000000);

        return obj;
    }
}
